/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.webserver.database;

import mb.config.*;
import mb.mbmiddle.MbPartMiddle;
import mb.mbpart.memory.BaseMemory;
import mb.mbpart.memory.QueryMemory;
import mb.utils.Utils;


/**
 *
 * @author thanh.nguyenchi
 */
public class QueryTopATMProcess extends BaseProcess{
   
    String lang;
    double lat;
    double lng;
    String sessionId;
    String atms;    // array city

    public QueryTopATMProcess(String lang,String lat, String lng,String sessionId){
        this.lang = lang;   
        this.lat = Utils.getDouble(lat);
        this.lng = Utils.getDouble(lng);
        this.sessionId = sessionId;
        process();
    }
    
    @Override
    public void process(){
        // get from memory
        String queryKey = this.sessionId + ConstantsMsg.STR_ATM_QUERY;
        // lay du lieu        
        QueryMemory objQueryMem = (QueryMemory)MbPartMiddle.objQueryMemory.get(queryKey);
        if (objQueryMem != null){
            // check time
            boolean isTime = objQueryMem.checkTime();
            if (isTime == true){
                // get from memory and return
                atms = objQueryMem.getData();
                return;
            }
        }
        // get from database
        SQLServerConnection conn = new SQLServerConnection();
        if (conn != null){
            if (conn.getStatus() == ConstantsMsg.INT_DATA_SUCESSFUL_CODE){
                atms = conn.getTopATM(lat, lng, sessionId);
            }
            else{
                status = ConstantsMsg.INT_NO_DATA_FOUND;
                error = ConstantsMsg.getErrMsg(status,lang); 
            }
        }
        else{
            status = ConstantsMsg.INT_NO_DATA_FOUND;
            error = ConstantsMsg.getErrMsg(status,lang);        
        }

        return;
    }
    
    @Override
    public String toJson(){ 
        String sResult = "";
        sResult = sResult + "{\"" + nStatus + "\":" + status;
        sResult = sResult + ",\"" + nError + "\":\"" + error + "\"";        
        sResult = sResult + ",\"" + nData + "\":" + atms;
        sResult = sResult + "}";
        return sResult;
    }
    
}

